package org.dromara.web.controller.platform;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.platform.domain.bo.TEnterpriseInfoBo;
import org.dromara.platform.domain.vo.TEnterpriseInfoVo;
import org.dromara.platform.mapper.TMiniProgramBaseInfoMapper;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.platform.domain.vo.TMiniProgramBaseInfoVo;
import org.dromara.platform.domain.bo.TMiniProgramBaseInfoBo;
import org.dromara.platform.service.ITMiniProgramBaseInfoService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 小程序基础信息
 *
 * @author Lion Li
 * @date 2025-06-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/platform/miniProgramBaseInfo")
public class TMiniProgramBaseInfoController extends BaseController {

    private final ITMiniProgramBaseInfoService tMiniProgramBaseInfoService;

    /**
     * 查询小程序基础信息列表
     */
    @SaCheckPermission("platform:miniProgramBaseInfo:list")
    @GetMapping("/list")
    public TableDataInfo<TMiniProgramBaseInfoVo> list(TMiniProgramBaseInfoBo bo, PageQuery pageQuery) {
        return tMiniProgramBaseInfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出小程序基础信息列表
     */
    @SaCheckPermission("platform:miniProgramBaseInfo:export")
    @Log(title = "小程序基础信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TMiniProgramBaseInfoBo bo, HttpServletResponse response) {
        List<TMiniProgramBaseInfoVo> list = tMiniProgramBaseInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "小程序基础信息", TMiniProgramBaseInfoVo.class, response);
    }

    /**
     * 获取小程序基础信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("platform:miniProgramBaseInfo:query")
    @GetMapping("/{id}")
    public R<TMiniProgramBaseInfoVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(tMiniProgramBaseInfoService.queryById(id));
    }

    /**
     * 新增小程序基础信息
     */
    @SaCheckPermission("platform:miniProgramBaseInfo:add")
    @Log(title = "小程序基础信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TMiniProgramBaseInfoBo bo) {
        return toAjax(tMiniProgramBaseInfoService.insertByBo(bo));
    }

    /**
     * 修改小程序基础信息
     */
    @SaCheckPermission("platform:miniProgramBaseInfo:edit")
    @Log(title = "小程序基础信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TMiniProgramBaseInfoBo bo) {
        return toAjax(tMiniProgramBaseInfoService.updateByBo(bo, LoginHelper.getUserId()));
    }

    /**
     * 删除小程序基础信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("platform:miniProgramBaseInfo:remove")
    @Log(title = "小程序基础信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(tMiniProgramBaseInfoService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 查询企业小程序下拉框列表
     */
    @SaCheckPermission("platform:enterpriseInfo:appList")
    @GetMapping("/getAppName")
    public List<TMiniProgramBaseInfoVo> getAppName(TMiniProgramBaseInfoBo bo) {
        return tMiniProgramBaseInfoService.getAppName(bo.getEnterpriseId());
    }

    /**
     * 查询小程序详情
     */
    @SaCheckPermission("platform:enterpriseInfo:appDetails")
    @GetMapping("/getAppDetails")
    public TMiniProgramBaseInfoVo getAppDetails(TMiniProgramBaseInfoBo bo) {
        return tMiniProgramBaseInfoService.getAppDetails(bo);
    }

    /**
     * 修改小程序名称
     */
    @SaCheckPermission("platform:miniProgramBaseInfo:edit")
    @Log(title = "小程序基础信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/setNickName")
    public R setNickName(@Validated(EditGroup.class) @RequestBody TMiniProgramBaseInfoBo bo) {
        return tMiniProgramBaseInfoService.setNickName(bo, LoginHelper.getUserId());
    }

    /**
     * 设置小程序介绍
     */
    @SaCheckPermission("platform:miniProgramBaseInfo:edit")
    @Log(title = "小程序基础信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/setSignature")
    public R setSignature(@Validated(EditGroup.class) @RequestBody TMiniProgramBaseInfoBo bo) {
        return tMiniProgramBaseInfoService.setSignature(bo, LoginHelper.getUserId());
    }

}
